//
// Created by Joy on 2024/7/22.
//

#include "iostream"

using namespace std;
//1.双链表数据类型
typedef struct Node{
    int data;
    struct Node * prior;
    struct Node * next;
}Node,*LinkList;
//2.初始化
Node* InitList(){
    Node *L = new Node;
    L->data = 0;
    L->prior = NULL;
    L->next = NULL;
    return L;
}
//3.双链表插入
int InsertList(Node *L,int value){ //插入链表的头和数据
    Node *p = new Node;
    p->data = value;
    p->next = L->next; //第一步：新结点next指向下一节点
    L->next = p; //第二步：新结点上一节点（next）指向新结点
    if (p->next != NULL){ //判断新结点知否为空
        L->next->prior = p; //下一节点的prior指向新结点
    }
    p->prior = L; //新结点prior指向上一节点
    return 0;
}
//4.打印链表
int NodeListPrint(Node *L){
    Node * p = L->next;
    cout << "链表的内容为：";
    while (p){
        cout << p->data;
        p = p->next;
    }
    cout << endl;
    return 0;
}
//5.删除节点
int DeleteNode(Node *L,int i){
    Node * p,* q;
    p = L->next;
    int j = 1;
    while (p && j < i -1){
        p = p->next;
        j++;
    }
    if (!p || j < i){
        return false;
    }
    q = p;
    p->prior->next = p->next;
    p->next->prior = p->prior;
    delete q;

}
int main() {
    Node *L = InitList();
    for (int i = 0; i < 10; ++i) {
        InsertList(L,i+1);
    }
    NodeListPrint(L);
    DeleteNode(L,1);
    NodeListPrint(L);
    return 0;
}
